import {
    mebeDetailApi,
    phoneCheckApi,
    checkCode,
    upLoadApi,
    loginOutApi,
    addressListApi,
    delAddressApi,
    feedBackApi,
    makeAddressApi,
    editAddressApi,
    editMemberApi,
    addressDetailApi,
    getInVoiceApi,
    delInVoiceApi,
    recharge,
    edtInvoiceApi,
    getBindCodeApi,
    putBindApi,
} from './api';
import { Toast } from 'antd-mobile';
import {routerRedux} from "dva/router";



export default {
    namespace: 'accountManagement',
    state: {
        list:[],
        invoiceList: {}
    },
    reducers:{
        save(state, { data }) {
            console.warn(state, '看看是否是所有的staste');
            return { ...state, ...data };
        },

        clearState(state) {
            return {list: [], invoiceList: {}};
        }
    },
    effects: {
        *DetaillEFF({ }, { put, call }) {
            const result = yield call(mebeDetailApi);
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret });
                Toast.hide();
            } else {

            }
        },
        *addressListEFF({ }, { put, call }) {
            const result = yield call(addressListApi);
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret });
                Toast.hide();
            } else {

            }
        },
        *loginOutEFF({ }, { put, call }) {
            const result = yield call(loginOutApi);
            yield put({ type: 'save', data: {}  });
            return result;
        },
        *upLoadEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(upLoadApi,obj);
            return result;
        },
        *delAddressEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(delAddressApi,obj);
            return result
        },
        *editAddressEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(editAddressApi,obj);
            return result
        },
        *addressDetailEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(addressDetailApi,obj);
            if (result.code == 200) {
                yield put({ type: 'clearState' });
                Toast.hide();
            } else {

            }
            return result
        },
        *makeAddressEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(makeAddressApi,obj);
           return result;
        },
        *editMemberEFF({obj }, { put, call }) {
            console.log("editMemberEFF");
            Toast.loading('加载中...', 3);
            const result = yield call(editMemberApi,obj);
            return result
        },

        // Lz........
        *getInVoiceEFF({}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(getInVoiceApi);
            if (result.code == 200) {
                yield put({ type: 'save', data:{ invoiceList: result.ret} });
                Toast.hide();
            } else {

            }
            return result
        },
        *delInVoiceEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(delInVoiceApi, obj);
            return result
        },

        *checkCodeEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(checkCode, obj);
            Toast.hide();
            return result
        },
        //获取绑定code
        *getBindCodeEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(getBindCodeApi, obj);
            Toast.hide();
            return result
        },
        // 提交绑定phone
        *putBindEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(putBindApi, obj);
            Toast.hide();
            return result
        },
        *rechargeEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(recharge, obj);
            if (result.code == 200) {
                console.log(result, "rechargeEFF..................");
                return result;
                // Toast.success(result.msg || "操作成功", 1.5,()=>{
                // });
                // Toast.hide();
            } else {

            }
            return result
        },
        *FeedbackEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(feedBackApi, obj);
            if (result.code == 200) {
               // Toast.success(result.msg, 1.5);
               return result
            } else {

            }
            return result
        },
        *PhoneCheckEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(phoneCheckApi, obj);
            if (result.code == 200) {
                 // Toast.success(result.msg, 1.5);
               return result
            } else {

            }
            return result
        },
        *edtInvoiceApiEFF({ obj }, { select ,put, call }) {
            console.log(obj)
            Toast.loading();
            const result = yield call(edtInvoiceApi,obj);
            if(result.code==200){
                Toast.hide();
                const invoiceList = yield select((state)=>state.accountManagement.invoiceList);
                let list = invoiceList.list.map((v,i,a)=>{
                    if(v.invId==obj.id ){
                        return { ...v, isDefault:1 }
                    }else{
                        return { ...v, isDefault:0 }
                    }
                })
                //zxl
                console.log(invoiceList, list, 'Lz,,,,')
                yield put({ type: 'save', data: {invoiceList: {list}} });
            }else {

            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if (pathname == '/accountManagement') {
                    dispatch({ type: 'DetaillEFF' });
                }
                if (pathname == '/myInvoice' || pathname == "/OrderInvoice" || pathname == "/invoice") {
                    dispatch({ type: 'getInVoiceEFF' });
                }
            });
        },
    },
};
